package com.heima.wemedia.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file) {
        String imgFile = null;
        //判断文件按是否为空
        if(file == null || file.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //上传文件到minio
        String UUID = IdUtil.fastSimpleUUID();
        String filename = file.getOriginalFilename();
        String filenName = filename.substring(filename.lastIndexOf("."));
        try {
            imgFile = fileStorageService.uploadImgFile("", UUID + filenName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());//用户ID
        wmMaterial.setUrl(imgFile);//图片地址
        wmMaterial.setType((short) 0);//0:图片
        wmMaterial.setIsCollection((short) 0);//0表示全部
        wmMaterial.setCreatedTime(new Date());//创建时间
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        Page<WmMaterial> pageList = this.lambdaQuery()
                .eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection())
                .page(page);
        ResponseResult result =new PageResponseResult(dto.getPage(),dto.getSize(),Integer.valueOf(String.valueOf(pageList.getTotal())));
        result.setData(pageList.getRecords());
        return result;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        List<WmNewsMaterial> wmNewsMaterials = wmNewsMaterialMapper.selectList(
                new QueryWrapper<WmNewsMaterial>().lambda().eq(WmNewsMaterial::getMaterialId, id)
        );
       if(CollUtil.isNotEmpty(wmNewsMaterials)){
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
       }
        this.lambdaUpdate()
                .eq(WmMaterial::getId,id)
                .remove();
        return ResponseResult.okResult(null);
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Integer id) {
        this.lambdaUpdate()
                .eq(WmMaterial::getId,id)
                .set(WmMaterial::getIsCollection,0)
                .update();
        return ResponseResult.okResult(null);
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        this.lambdaUpdate()
                .eq(WmMaterial::getId,id)
                .set(WmMaterial::getIsCollection,1)
                .update();
        return ResponseResult.okResult(null);
    }
}
